package com.example.testcalendar;

import java.util.Calendar;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract.Instances;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class EventTesting extends Activity {

    private final String dateFormat = "yyyy-MM-dd HH:mm";

    private TextView text;
    private Button show;
    private Button add;
    private Button del;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_testing);

        text = (TextView) findViewById(R.id.text3);
        show = (Button) findViewById(R.id.show);
        add = (Button) findViewById(R.id.add);
        del = (Button) findViewById(R.id.delete);

    }

    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.show:
            Toast.makeText(this, "show", Toast.LENGTH_SHORT).show();
            show();
            break;
        case R.id.add:
            Toast.makeText(this, "add", Toast.LENGTH_SHORT).show();

            break;
        case R.id.delete:
            Toast.makeText(this, "delete", Toast.LENGTH_SHORT).show();

            break;
        }
    }

    private void show() {

        long eventId;
        String title;
        String start;
        long calId;
        String calName;
        int count;

        String result;

        Cursor cur = null;
        ContentResolver cr = getContentResolver();

        Calendar beginTime = Calendar.getInstance();
        beginTime.set(2015, 1, 1, 0, 0);
        long startMillis = beginTime.getTimeInMillis();
        Calendar endTime = Calendar.getInstance();
        endTime.set(2015, 3, 1, 0, 0);
        long endMillis = endTime.getTimeInMillis();

        Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
        ContentUris.appendId(builder, startMillis);
        ContentUris.appendId(builder, endMillis);

        cur = cr.query(builder.build(), Query.INSTANCE_PROJECTION, null, null, "Instances.BEGIN asc limit 10");

        count = cur.getCount();

        result = "Total No. of result: " + count;
        while (cur.moveToNext()) {
            eventId = cur.getLong(Query.INSTANCE_ID);
            title = cur.getString(Query.INSTANCE_TITLE);
            start = (String) DateFormat.format(dateFormat, cur.getLong(Query.INSTANCE_BEGIN));
            calId = cur.getLong(Query.INSTANCE_CALENDAR_ID);
            calName = cur.getString(Query.INSTANCE_CALENDAR_NAME);

            result = result + "\n\nEvent ID: " + eventId + "\nTitle: " + title + "\nStart: " + start + "\nCalendar ID: "
                    + calId + "\nCalendar Name: " + calName;
        }
        text.setText(result);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.event_testing, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
